package com.cjst.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 只用于产生URL+PARAMS 只返回String
 * 
 * @author CXB *
 */
public class BaiduMapUtil {

	private Logger log = Logger.getLogger(BaiduMapUtil.class);

	private static final String ak = "q1M4Gul17RygafHDIKRWbIqI";
	private static final String default_place_url = "http://api.map.baidu.com/place/v2/";
	private static final String default_walking_url = "http://api.map.baidu.com/directionlite/v1/";
	private static final String output = "json";
	private static final Map<String, Object> default_place_params = new HashMap<String, Object>(8);
	private static final Map<String, Object> default_walking_params = new HashMap<String, Object>(8);
	static {
		default_place_params.put("ak", ak);
		default_place_params.put("output", output);

		default_walking_params.put("ak", ak);

	}

	// http://api.map.baidu.com/place/v2/search?query=ATM机&region=北京&output=json&ak=您的ak
	// //GET请求 行政区划区域检索
	// http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=xml&ak=您的密钥
	// //GET请求 圆形区域检索
	// http://api.map.baidu.com/place/v2/search?query=银行&bounds=39.915,116.404,39.975,116.414&output=json&ak={您的密钥}
	// //GET请求 矩形区域检索

	// http://api.map.baidu.com/place/v2/detail?uid=435d7aea036e54355abbbcc8&output=json&scope=2&ak=您的密钥
	// //GET请求 地点详情检索服务

	// http://api.map.baidu.com/place/v2/suggestion?query=天安门&region=北京&city_limit=true&output=json&ak=你的ak
	// //GET请求 地点输入提示服务

	// 步行路线规划
	// http://api.map.baidu.com/directionlite/v1/walking?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK
	// //GET请求

	/**
	 * 行政区划区域检索
	 * http://api.map.baidu.com/place/v2/search?query=ATM机&region=北京&output=json&ak=您的ak
	 * //GET请求
	 * 
	 * @param query
	 * @param region
	 * @return
	 */
	public static String getSearchByRegion(String query, String region) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_place_params);
		params.put("query", query);
		params.put("region", region);
		return default_place_url + "search" + "?" + urlencode(params);
	}

	/**
	 * 圆形区域检索
	 * http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=xml&ak=您的密钥
	 * //GET请求
	 * 
	 * @param query
	 * @param location
	 * @param radius
	 * @return
	 */

	public static String getSearchByRadius(String query, String location, String radius) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_place_params);
		params.put("location", location);
		params.put("radius", radius);
		return default_place_url + "search" + "?" + urlencode(params);
	}

	/**
	 * 矩形区域检索
	 * http://api.map.baidu.com/place/v2/search?query=银行&bounds=39.915,116.404,39.975,116.414&output=json&ak={您的密钥}
	 * //GET请求
	 * 
	 * @param query
	 * @param bounds
	 * @return
	 */
	//
	public static String getSearchByBounds(String query, String bounds) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_place_params);
		params.put("query", query);
		params.put("bounds", bounds);
		return default_place_url + "search" + "?" + urlencode(params);
	}

	/**
	 * 地点详情检索服务
	 * http://api.map.baidu.com/place/v2/detail?uid=435d7aea036e54355abbbcc8&output=json&scope=2&ak=您的密钥
	 * //GET请求
	 * 
	 * @param uid
	 * @param scope
	 * @return
	 */
	public static String getdetailByUid(String uid, String scope) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_place_params);
		params.put("uid", uid);
		params.put("scope", scope);
		return default_place_url + "detail" + "?" + urlencode(params);
	}

	/**
	 * 地点输入提示
	 * http://api.map.baidu.com/place/v2/suggestion?query=天安门&region=北京&city_limit=true&output=json&ak=你的ak
	 * 
	 * @param query
	 * @param region
	 * @param city_limit
	 * @return
	 */
	// http://api.map.baidu.com/place/v2/suggestion?query=天安门&region=北京&city_limit=true&output=json&ak=你的ak
	// //GET请求 地点输入提示服务
	public static String getSuggestion(String query, String region, String city_limit) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_place_params);
		params.put("query", query);
		params.put("region", region);
		params.put("city_limit", city_limit);
		return default_place_url + "suggestion" + "?" + urlencode(params);
	}

	public static String urlencode(Map<String, Object> data) {
		// 将map里的参数变成像 one=###&two=###&的样子
		StringBuilder sb = new StringBuilder();
		for (Map.Entry i : data.entrySet()) {
			try {
				sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * origin=40.01116,116.339303 destination=39.936404,116.452562
	 * 
	 * @param origin
	 * @param destination
	 * @return
	 */
	// 步行路线规划
	// http://api.map.baidu.com/directionlite/v1/walking?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK
	// //GET请求
	public static String getWalkingRoute(String origin, String destination) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_walking_params);
		params.put("origin", origin);
		params.put("destination", destination);
		return default_walking_url + "walking" + "?" + urlencode(params);
	}
	
	//http://api.map.baidu.com/panorama/v2?ak=E4805d16520de693a3fe707cdc962045&width=512&height=256&location=116.313393,40.04778&fov=180  
	
	public static String getPanoView(int width,int height,String location ,int fov ) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(default_walking_params);
		params.put("width", width);
		params.put("height", height);
		params.put("location", location);
		params.put("fov", fov);
		return "http://api.map.baidu.com/panorama/v2?"+urlencode(params);
	}

}
